﻿using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;

namespace Codemonk.Updater.Server
{
    internal static class ServerConfig
    {
        private const string ConfigFile = "codemonk.updater.server.xml";
        static ServerConfig()
        {
            XmlDocument document = new XmlDocument();
            var basedir = Path.GetDirectoryName(typeof(ServerConfig).Assembly.CodeBase).Substring("file://".Length - 1);
            document.Load(Path.Combine(basedir, ConfigFile));
            Func<string, string> readValue = (xpath) => { var node = document.SelectSingleNode(xpath); return node == null ? string.Empty : node.InnerText; };
            WorkDir = Path.GetFullPath(Path.Combine(basedir, readValue("/codemonk.updater.server/workdir")));
            ConfigDir = readValue("/codemonk.updater.server/configdir");
            UpdatelistFile = readValue("/codemonk.updater.server/updatelistfile");
            CompulsiveFile = readValue("/codemonk.updater.server/compulsivefile");
            EmptyDescrption = readValue("/codemonk.updater.server/emptydescrption");
            EntryFile = readValue("/codemonk.updater.server/entryfile");
            SearchPattern = new Regex(readValue("/codemonk.updater.server/searchpattern"), RegexOptions.Compiled);
            SearchOption = readValue("/codemonk.updater.server/searchoption") == "toponly" ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories;
        }

        internal static string WorkDir { get; private set; }
        internal static string ConfigDir { get; private set; }
        internal static string UpdatelistFile { get; private set; }
        internal static string CompulsiveFile { get; private set; }
        internal static string EmptyDescrption { get; private set; }
        internal static string EntryFile { get; private set; }
        internal static Regex SearchPattern { get; private set; }
        internal static SearchOption SearchOption { get; private set; }
    }
}
